import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      '@houduan': path.resolve(__dirname, '../houduan'),
      '@houduan/': path.resolve(__dirname, '../houduan/')
    }
  },
  server: {
    host: '0.0.0.0',  // 允许所有IP访问
    port: 5173,
    strictPort: false,  // 如果端口被占用，自动尝试下一个端口
    allowedHosts: [
      'localhost',
      '127.0.0.1',
      '537e7f45.r9.cpolar.top',
      'lutiance-1371863414.cos-website.ap-beijing.myqcloud.com'
    ],
    
    // 开发环境配置
    hmr: {
      overlay: true  // 显示错误覆盖层
    },
    
    // 代理配置
    proxy: {
      '/zjx': {
        target: 'http://localhost:3006',
        changeOrigin: true,
        configure: (proxy, options) => {
          proxy.on('proxyReq', (proxyReq, req, res) => {
            // 增加请求体大小限制
            if (req.body) {
              const bodyData = JSON.stringify(req.body);
              proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
              proxyReq.write(bodyData);
            }
          });
        }
      }
      // 可以在这里配置其他API代理
      // '/api': {
      //   target: 'http://localhost:3005',
      //   changeOrigin: true,
      //   rewrite: (path) => path.replace(/^\/api/, '')
      // }
    }
  },
  
  // 构建配置
  build: {
    outDir: 'dist',
    sourcemap: false,  // 生产环境不生成sourcemap
    rollupOptions: {
      output: {
        manualChunks: {
          vendor: ['react', 'react-dom'],
          router: ['react-router-dom']
        }
      }
    }
  },
  
  // 环境变量配置
  define: {
    __DEV__: process.env.NODE_ENV === 'development'
  }
})
